Makes a new array that is a copy of source: the same number of places, holding the same values. Changing the copy never changes source, and the other way round. It goes on the right-hand side of the line that makes the variable, like array_create, and it costs a whole second array of memory.
array_copy(source) -> array<T>| Name | Type |
|---|---|
source | array |
array<T>
Makes a new array that is a copy of source: the same number of places, holding the same values. Changing the copy never changes source, and changing source never changes the copy.
var backup = array_copy(source_array);The copy has exactly as many places as source has. An array made with array_create(5, 0) makes a copy with five places, and an array written out as [10, 20, 30] makes a copy with three. If source was given more room than values (/// @capacity 5 over [10, 20, 30]), the copy has the same three places and the same room for five, and array_length says 3 for both.
A copy takes its size from source, so there is no size for you to write. A copy given a smaller size of its own could not hold everything the original holds, so putting a size beside one (var backup: array<int, 4> = array_copy(source);, or a /// @capacity tag) is refused when the project is checked.
A copy is a second whole array. The Game Boy Advance has no spare memory to hand out while a game runs, so the room for the copy is set aside when the game is built, in full, whether or not it ever fills up. An array with room for 200 numbers makes a copy that takes room for 200 numbers. Two copies take the room of two. A copy in an object's variable keeps its room for as long as the object exists, even when nothing uses it any more.
Making the copy takes time as well. It walks through every place that source holds and copies each one, so the time grows with how many there are. A few places copied when something happens - a button press, a new level - is nothing. Hundreds of places copied twice in every frame is a real part of the frame spent copying, and it adds up on a real cartridge. Copy when you need to, not every frame.
Where the room comes from depends on where the variable lives:
backup = array_copy(source);), the copy is a variable of the object, and every instance of the object has its own. An object that can have 30 instances at once has 30 copies.var (var backup = array_copy(source);), the copy lives only while the event runs, on the console's stack - which is small. Keep a copy made this way small, and keep a big one in an object's variable instead.A copy is a new container, and its size belongs to the variable that holds it - so it goes in the same two places array_create does, and nowhere else:
var: var backup = array_copy(source);backup = array_copy(source);Not inside another call or a sum, not on a line by itself, not in a later event such as Step, and not over a variable that already exists - that variable already has a size, and a copy of a different size cannot be put in it. To fill an array from another one later in the game, set its places one at a time in a loop: for (var i = 0; i < array_length(a); i++) { a[i] = b[i]; }.
source has to be a variable written on its own, like array_copy(scores), whose size the compiler knows: one made in the same event, or an object variable made in Create. Anything else is refused with a plain message:
array_copy(self.items)). Put it in a variable first.Giving it the wrong kind of container is refused too: array_copy copies an array and nothing else.
Arrays have array_length too, for counting the places in the copy or the original.
var scores = array_create(5, 0);
scores[2] = 99;
var backup = array_copy(scores); // five places, and the third one holds 99
backup[2] = 0; // changes only the copy: scores[2] is still 99A copy of the fixed-size vector - the new one has the same N as source (its declared size), is filled with copies of the same items, and shares nothing with it. A copy is a second whole container, so it costs a second container's worth of memory: as many places as the original has room for (its capacity, not just how many it holds right now), and copying is O(n) in how many items the original holds. The Game Boy Advance has no heap, so the copy's size is fixed when the game is built - it is the original's own declared size, which is why the original has to be a variable whose size is known here (otherwise it is refused: a script's parameter could be any size). Like a creation call it has to be the whole right-hand side of the line that makes a variable, and for an object's variable that line is in its Create event, where every instance of the object gets its own copy of the memory. A copy is always the same size as the original, so a size written beside it is refused.